// 生成日期数组的纯函数
function generateDateArray(selectedNumber) {
    console.log("进入  生成日期数组方法");
    var today = new Date();
    var dateArray = [];
    for (var i = 0; i < selectedNumber; i++) {
        var currentDate = new Date(today);
        currentDate.setDate(today.getDate() + i);
        var year = currentDate.getFullYear();
        var month = currentDate.getMonth() + 1;
        if (month < 10) {
            month = '0' + month;
        }
        var day = currentDate.getDate();
        if (day < 10) {
            day = '0' + day;
        }
        dateArray.push(year + '-' + month + '-' + day);
    }
    return dateArray;
}


// 发起 POST 请求的纯函数
const postData = (url, data, callback ) => {
    return fetch(url, {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json'
        },
        body: JSON.stringify(data)
    })
    .then(response => response.json())
    .then(result => {
        console.log("tools postData ",result);
        // console.log("data tools  ",data);
        if (callback) {
            callback(result);
        }
        return result;
    })
    .catch(error => {
        console.error('Error:', error);
        throw error;
    });
};



// 从表单获取数据的纯函数
function getFormData(formId) {
    var form = document.getElementById(formId);
    var formData = new FormData(form);
    var data = {};
    var entries = formData.entries();
    var entry;
    while ((entry = entries.next()) && !entry.done) {
        var key = entry.value[0];
        var value = entry.value[1];
        console.log("getFormData  key :", key, "value :", value);
        data[key] = value;
    }
    return data;
}



// 根据id找元素 修改元素的值 修改参数默认值为 true，即默认不可点击
function setInputValueById(id, text, isDisabled = true) {
    const inputElement = document.getElementById(id);
    if (inputElement) {
        inputElement.value = text;
        // 根据 isDisabled 的值设置按钮是否可点击
        inputElement.disabled = isDisabled;
    } else {
        console.error(`未找到 id 为 ${id} 的 input 元素`);
    }
}



function showAlert(message) {
     // 创建弹窗元素
                const popup = document.createElement('div');
                popup.className = 'popup';

                const popupContent = document.createElement('div');
                popupContent.className = 'popup-content';

                const closeBtn = document.createElement('span');
                closeBtn.className = 'close';
                closeBtn.innerHTML = '&times;';
                closeBtn.onclick = function () {
                    popup.style.display = 'none';
                };

                const msg = document.createElement('p');
                msg.textContent = message;

                popupContent.appendChild(closeBtn);
                popupContent.appendChild(msg);
                popup.appendChild(popupContent);

                document.body.appendChild(popup);

                popup.style.display = 'flex';

                // 设置 2 秒后自动关闭弹窗
                setTimeout(() => {
                    popup.style.display = 'none';
                }, 2000);

}